package jiezi.game 
{
	import flash.display.Sprite;
	import jiezi.game.SequenceImageInfo
	import flash.events.Event;
	import jiezi.game.jzTextureManager;
	
	/**
	 * ...
	 * @author ...
	 */
	public class jzSprite extends Sprite 
	{
		private var mSeqImageInfo:SequenceImageInfo = new SequenceImageInfo;
		
		private var mCurrentFrame:Number = 0;
		private var mTimer:Number = 0.0;
		private var mSPF:Number = 0.05;
		private var mNumFrames:int = 1;
		private var mAction:int = 0;
		private var mDir:int = 0;
		private var mActionOrDirDirty:Boolean = false;

		
		public function jzSprite() 
		{
		}
		
		public function setSequenceID(id:int):void
		{
			mSeqImageInfo.setSequenceID(id)
		}
		
		public function setAction(act:int):void
		{
			mAction = act;
			mCurrentFrame = 0;
			mActionOrDirDirty = true;
		}
		
		public function setDir(dir:int):void
		{
			mDir = dir;
			mActionOrDirDirty = true;
		}
		
		private function updateSequenceInfos():void
		{
			var dirinfo:* = mSeqImageInfo.getDirInfo(mAction, mDir);
			mNumFrames = dirinfo.numframes;
			mSPF = dirinfo.duration / mNumFrames;
			mActionOrDirDirty = false;
		}
		
		public function update(elapsedT:Number):void
		{
			if ( mSeqImageInfo.completed ) {
				if ( mActionOrDirDirty ) {
					updateSequenceInfos();
				}
				mTimer += elapsedT;
				while ( mTimer >= mSPF ) {
					var fn:String = mSeqImageInfo.getDirInfo(mAction, mDir).files[mCurrentFrame];
					var tex:* = jzTextureManager.getSingleton().getTexture(fn);
					if ( tex ) {
						if ( this.numChildren > 0 ) {
							this.removeChild(this.getChildAt(0));							
						}
						this.addChild(tex);
					}
					mTimer -= mSPF;
					mCurrentFrame += 1;
					mCurrentFrame %= mNumFrames;
				}
			}
		}
	}

}